package com.jp.knowledge.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foamtrace.photopicker.PhotoPickerActivity;
import com.foamtrace.photopicker.PhotoPreviewActivity;
import com.foamtrace.photopicker.SelectModel;
import com.foamtrace.photopicker.intent.PhotoPickerIntent;
import com.foamtrace.photopicker.intent.PhotoPreviewIntent;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.team_circle.TeamCircleCreateImageAdapter;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.dialog.TeamCircleCreateCompanySelectDialog;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.logic.UploadLogic;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.TeamCircleConstactsModel;
import com.jp.knowledge.model.TeamCirclePushParams;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.model.attention.CompanyListModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.my.model.SearchResultModel;
import com.jp.knowledge.my.util.MyUtil;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.MultipleImageUploader;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.JpRecycleView;

import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class TeamCircleCreateActivity extends SlidingActivity implements View.OnClickListener, RecycleVAdapter.OnItemClickListener, RecycleVAdapter.OnItemLongClickListener, MultipleImageUploader.ImageUploadCallBack, ResultSubscriber.OnResultListener {

    private static final int REQUEST_IMAGE_CODE = 0x01;
    private static final int REQUEST_PREVIEW_CODE = 0x02;
    private static final int SET_VISIBILITY_CODE = 0x03;
    private static final int SET_REMIND_CODE = 0x04;
    private static final int SET_SENDTO_CODE = 0x05;
    protected static final int PUSH_DYNAMIC_CODE = 0x06;

    /* 启动 Activity 时 intent 传入来的参数 */
    public static final String INTENT_TYPE = "type";                // 必须
    public static final String INTENT_LEVEL = "level";              // 必须
    public static final String COMPANY_ID = "companyId";
    public static final String COMPANY_TYPE = "companyType";
    public static final String COMPANY_NAME = "companyName";
    public static final String INTENT_LINK_ICON = "linkIcon";
    public static final String INTENT_LINK_ID = "linkId";
    public static final String INTENT_LINK_TITLE = "linkTitle";
    public static final String INTENT_LINKK_TYPE = "linkType";

    @ViewInject(R.id.team_circle_create_edit_content)
    protected EditText contentEdit;
    @ViewInject(R.id.team_circle_create_add_img)
    private JpRecycleView imageAddRv;
    @ViewInject(R.id.team_circle_link_view)
    private LinearLayout linkView;
    @ViewInject(R.id.team_circle_linkIcon)
    private ImageView linkIcon;
    @ViewInject(R.id.team_circle_linkTitle)
    private TextView linkTitle;

    @ViewInject(R.id.team_circle_create_level)
    protected LinearLayout itemLevel;
    private ImageView itemLevelLeftIcon;
    private TextView itemLevelLeftText;
    private TextView itemLevelRightText;
    @ViewInject(R.id.team_circle_create_toCompany)
    protected LinearLayout itemToCompany;
    private ImageView itemToCompanyLeftIcon;
    private TextView itemToCompanyLeftText;
    private TextView itemToCompanyRightText;
    @ViewInject(R.id.team_circle_create_visibility)
    protected LinearLayout itemVisibility;
    private ImageView itemVisibilityLeftIcon;
    private TextView itemVisisbilityLeftText;
    private TextView itemVisisbilityRightText;
    @ViewInject(R.id.team_circle_create_remind)
    protected LinearLayout itemRemind;
    private ImageView itemRemindLeftIcon;
    private TextView itemRemindLeftText;
    private TextView itmeRemindRightText;

    private List<String> imagePaths;
    private TeamCircleCreateImageAdapter imageAddAdapter;
    private Dialog levelSelectDialog;
    private CommDialog deletePhotoDialog;
    private CommDialog returnDialog;
    private TeamCircleCreateCompanySelectDialog companySelectDialog;
    private MultipleImageUploader imageUploader;

    private List<TeamCircleConstactsModel> visibilityConstactsModels;
    private List<SearchResultModel> visibilityCompanyModels;
    private List<TeamCircleConstactsModel> remindConstactsModels;
    private HashMap<String, ArrayList<TeamCircleConstactsModel>> selectConstacts;

    private Intent intent;
    private String companyId;
    private int companyType;
    private String companyName;
    protected TeamCirclePushParams paramsBean;                // 点发表是向 http 请求中提交的参数

    @Override
    protected int getResourceId() {
        return R.layout.activity_team_circle_create;
    }

    @Override
    protected void init() {
        initData();
        initTitle();
        initView();
        initDialog();
    }

    // 设置参数
    private void initData(){
        intent = getIntent();
        paramsBean = new TeamCirclePushParams();
        paramsBean.setToken(HttpHelper.token);
        paramsBean.setType(intent.getIntExtra(INTENT_TYPE, 1));
        paramsBean.setLevel(intent.getIntExtra(INTENT_LEVEL, 0));
        paramsBean.setLinkType(intent.getIntExtra(INTENT_LINKK_TYPE, 1));
        paramsBean.setLinkId(intent.getStringExtra(INTENT_LINK_ID));
        paramsBean.setLinkIcon(intent.getStringExtra(INTENT_LINK_ICON));
        paramsBean.setLinkTitle(intent.getStringExtra(INTENT_LINK_TITLE));
        paramsBean.setVisibility(1);
        if(intent.getStringExtra(COMPANY_ID) != null) {
            companyId = intent.getStringExtra(COMPANY_ID);
            companyType = intent.getIntExtra(COMPANY_TYPE, 1);
            companyName = intent.getStringExtra(COMPANY_NAME);
            paramsBean.setCompanyId(companyId);
            paramsBean.setCompanyType(companyType);
        } else{
            try{
                UserData userData = application.getUserData();
                companyId = userData.getCompanyList().get(0).getCompanyId();
                companyType = userData.getCompanyList().get(0).getCompanyType();
                companyName = userData.getCompanyList().get(0).getCompanyName();
                paramsBean.setCompanyId(companyId);
                paramsBean.setCompanyType(companyType);
            } catch (Exception e){}
        }
        imagePaths = new ArrayList();
        visibilityConstactsModels = new ArrayList<>();
        visibilityCompanyModels = new ArrayList<>();
        remindConstactsModels = new ArrayList<>();
        selectConstacts = new HashMap<>();
        imageUploader = new MultipleImageUploader(this);
        imageUploader.setImageUploadCallBack(this);
    }

    protected void initView(){
        itemLevelLeftIcon = (ImageView) itemLevel.findViewById(R.id.left_icon);
        itemLevelLeftText = (TextView) itemLevel.findViewById(R.id.left_text);
        itemLevelRightText = (TextView) itemLevel.findViewById(R.id.right_text);
        itemToCompanyLeftIcon = (ImageView) itemToCompany.findViewById(R.id.left_icon);
        itemToCompanyLeftText = (TextView) itemToCompany.findViewById(R.id.left_text);
        itemToCompanyRightText = (TextView) itemToCompany.findViewById(R.id.right_text);
        itemVisibilityLeftIcon = (ImageView) itemVisibility.findViewById(R.id.left_icon);
        itemVisisbilityLeftText = (TextView) itemVisibility.findViewById(R.id.left_text);
        itemVisisbilityRightText = (TextView) itemVisibility.findViewById(R.id.right_text);
        itemRemindLeftIcon = (ImageView) itemRemind.findViewById(R.id.left_icon);
        itemRemindLeftText = (TextView) itemRemind.findViewById(R.id.left_text);
        itmeRemindRightText = (TextView) itemRemind.findViewById(R.id.right_text);
        itemLevelLeftIcon.setImageResource(R.mipmap.team_circle_importantdegree);
        itemToCompanyLeftIcon.setImageResource(R.mipmap.team_circle_publishto);
        itemVisibilityLeftIcon.setImageResource(R.mipmap.team_circle_open);
        itemRemindLeftIcon.setImageResource(R.mipmap.aite);
        itemLevelLeftText.setText("重要程度");
        itemToCompanyLeftText.setText("发表到");
        itemVisisbilityLeftText.setText("部分可看");
        itemRemindLeftText.setText("提醒谁看");
        itemVisisbilityRightText.setText("全部人员");
        setLevel(paramsBean.getLevel());
        itemToCompanyRightText.setText(companyName);
        itmeRemindRightText.setText("");
//        itemLevel.setVisibility(View.VISIBLE);
        itemLevel.setVisibility(View.GONE);
        contentEdit.setHint("我想说的是...");

        imageAddRv.setLayoutManager(new GridLayoutManager(this, 4));
        imageAddRv.setHasFixedSize(true);
        imageAddAdapter = new TeamCircleCreateImageAdapter(this, new ArrayList<String>(){{add("");}});
        imageAddRv.setAdapter(imageAddAdapter);
        imageAddAdapter.addItemClickListener(this);
        imageAddAdapter.addItemLongClickListener(this);
        linkView.setOnClickListener(this);
        if(!TextUtils.isEmpty(paramsBean.getLinkIcon())){
            ImageLoadUtil.load(this, paramsBean.getLinkIcon(), linkIcon);
            linkIcon.setVisibility(View.VISIBLE);
        }else{
            linkIcon.setVisibility(View.GONE);
        }

        linkTitle.setText(paramsBean.getLinkTitle());
        if(paramsBean.getType() == 1 || paramsBean.getType() == 2){
            imageAddRv.setVisibility(View.VISIBLE);
            linkView.setVisibility(View.GONE);
        } else{
            imageAddRv.setVisibility(View.GONE);
            linkView.setVisibility(View.VISIBLE);
        }

        itemLevel.setOnClickListener(this);
        itemToCompany.setOnClickListener(this);
        itemVisibility.setOnClickListener(this);
        itemRemind.setOnClickListener(this);

        if(getIntent().getStringExtra(COMPANY_ID) == null){
            itemLevel.setVisibility(View.GONE);
            itemToCompany.setVisibility(View.GONE);
            itemVisibility.setVisibility(View.GONE);
            itemRemind.setVisibility(View.GONE);
        }
    }

    private void initTitle() {
        leftIcon.setVisibility(View.VISIBLE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);
        topDesc.setVisibility(View.GONE);
        rightIcon.setVisibility(View.GONE);
        rightBtn.setText("发表");
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setOnClickListener(this);
        if(paramsBean.getLevel() == 0){
            topName.setText("发表动态");
        } else{
            topName.setText("我要爆料");
        }
        topName.setVisibility(View.GONE);
    }

    private void initDialog(){
        levelSelectDialog = new Dialog(this, R.style.custom_idalgo);
        View levelDialogView = View.inflate(this, R.layout.team_circle_create_level_dialog, null);
        levelDialogView.findViewById(R.id.team_circle_level_normal).setOnClickListener(this);
        levelDialogView.findViewById(R.id.team_circle_level_important).setOnClickListener(this);
        levelDialogView.findViewById(R.id.team_circle_level_urgent).setOnClickListener(this);
        levelSelectDialog.setContentView(levelDialogView);
        if(paramsBean.getLinkId() != null)
            ((TextView)levelDialogView.findViewById(R.id.team_circle_level_normal)).setText("日常分享");
        levelDialogView.findViewById(R.id.team_circle_level_important).setVisibility(View.GONE);

        deletePhotoDialog = new CommDialog(this);
        deletePhotoDialog.setContent("是否删除图片");
        deletePhotoDialog.setLeft("取消");
        deletePhotoDialog.setRight("确定");
        deletePhotoDialog.setRightListener(this);

        returnDialog = new CommDialog(this);
        returnDialog.setContent("是否退出此次编辑？");
        returnDialog.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishSelf();
            }
        });

        companySelectDialog = new TeamCircleCreateCompanySelectDialog(mContext);
        companySelectDialog.setCompanySelectedListener(new TeamCircleCreateCompanySelectDialog.CompanySelectedListener() {
            @Override
            public void onCompanySelected(String companyId, String companyName, int companyType) {
                TeamCircleCreateActivity.this.companyId = companyId;
                TeamCircleCreateActivity.this.companyName = companyName;
                TeamCircleCreateActivity.this.companyType = companyType;
                itemToCompanyRightText.setText(companyName);

                remindConstactsModels.clear();
                ArrayList<TeamCircleConstactsModel> selectContactList = selectConstacts.get(companyId);
                if(selectContactList != null && selectContactList.size() > 0)
                    remindConstactsModels.addAll(selectContactList);
                setVisibilityText();
            }
        });
    }

    private void setVisibilityText(){
        String result = "";
        for(int i = 0; i < remindConstactsModels.size(); i++){
            result += remindConstactsModels.get(i).getUserName();
            if(i < remindConstactsModels.size()-1)
                result += "、";
        }
        if(result.trim().length() > 0)
            itemVisisbilityRightText.setText(result);
        else
            itemVisisbilityRightText.setText("全部人员");
    }

    private void setLevel(int level){
        Drawable drawable = mContext.getResources().getDrawable(R.mipmap.faxiangengduo);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        if(level == 1){
            itemLevelRightText.setTextColor(getResources().getColor(R.color.font_gray_deep));
            if(paramsBean.getLinkId() != null)
                itemLevelRightText.setText("日常分享");
            else
                itemLevelRightText.setText("日常动态");
//            itemToCompanyRightText.setCompoundDrawables(null, null, null, null);
//            itemToCompanyRightText.setPadding(0, 0, (int) getResources().getDimension(R.dimen.space_big), 0);
            itemVisibility.setVisibility(View.VISIBLE);
        } else if(level == 2){
            itemLevelRightText.setTextColor(getResources().getColor(R.color.google_red));
            itemLevelRightText.setText("重要爆料");
//            itemToCompanyRightText.setCompoundDrawables(null, null, drawable, null);
//            itemToCompanyRightText.setPadding(0, 0, 0, 0);
            itemVisibility.setVisibility(View.GONE);
        } else if(level == 3){
            itemLevelRightText.setTextColor(getResources().getColor(R.color.google_red));
            itemLevelRightText.setText("紧急爆料");
//            itemToCompanyRightText.setCompoundDrawables(null, null, drawable, null);
//            itemToCompanyRightText.setPadding(0, 0, 0, 0);
            itemVisibility.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != RESULT_OK || data == null)
            return;
        if(requestCode == REQUEST_IMAGE_CODE) {
            imagePaths.clear();
            imagePaths.addAll(data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT));
            imageAddAdapter.updateAdapter(imagePaths);
        } else if(requestCode == REQUEST_PREVIEW_CODE){
            imagePaths.clear();
            imagePaths.addAll(data.getStringArrayListExtra(PhotoPreviewActivity.EXTRA_RESULT));
            imageAddAdapter.updateAdapter(imagePaths);
        } else if(requestCode == SET_SENDTO_CODE){
            paramsBean.setVisibility(data.getIntExtra(TeamCircleSelectVisibilityActivity.VISIBILITY_TYPE, 1));
            companyId = data.getStringExtra(TeamCircleCreateActivity.COMPANY_ID);
            companyName = data.getStringExtra(TeamCircleCreateActivity.COMPANY_NAME);
            companyType = data.getIntExtra(TeamCircleCreateActivity.COMPANY_TYPE, 1);
            visibilityConstactsModels = (List<TeamCircleConstactsModel>)
                    data.getSerializableExtra(TeamCircleSelectVisibilityActivity.TEAM_CIRCLE_CONSTACTS_MODEL_LIST);
            visibilityCompanyModels = (List<SearchResultModel>)
                    data.getSerializableExtra(TeamCircleSelectVisibilityActivity.TEAM_CIRCLE_COMPANY_MODEL_LIST);
            if(paramsBean.getVisibility() == 1) {
                itemToCompanyRightText.setText(companyName);
                itemVisibility.setVisibility(View.VISIBLE);
            } else {
                itemToCompanyRightText.setText(visibilityCompanyModels.get(0).getName());
                itemVisibility.setVisibility(View.GONE);
            }
        } else if(requestCode == SET_VISIBILITY_CODE){
//            paramsBean.setVisibility(data.getIntExtra(TeamCircleSelectVisibilityActivity.VISIBILITY_TYPE, 1));
//            visibilityConstactsModels = (List<TeamCircleConstactsModel>)
//                    data.getSerializableExtra(TeamCircleSelectVisibilityActivity.TEAM_CIRCLE_CONSTACTS_MODEL_LIST);
//            visibilityCompanyModels = (List<SearchResultModel>)
//                    data.getSerializableExtra(TeamCircleSelectVisibilityActivity.TEAM_CIRCLE_COMPANY_MODEL_LIST);
//            if(paramsBean.getVisibility() == 1)
//                itemVisisbilityRightText.setText("全部人员");
//            else
//                itemVisisbilityRightText.setText("部分可见");

            remindConstactsModels.clear();
            ArrayList<TeamCircleConstactsModel> selectContactList = (ArrayList)data.getSerializableExtra(TeamCircleConstactsActivity.SELECT_CONSTACTS_DATA);
            if(selectContactList != null && selectContactList.size() > 0)
                remindConstactsModels.addAll(selectContactList);
            selectConstacts.put(data.getStringExtra(TeamCircleConstactsActivity.COMPANY_ID), selectContactList);
            setVisibilityText();
        } else if(requestCode == SET_REMIND_CODE){
            remindConstactsModels = (List<TeamCircleConstactsModel>)
                    data.getSerializableExtra(TeamCircleConstactsActivity.SELECT_CONSTACTS_DATA);
            String result = "";
            for(int i = 0; i < remindConstactsModels.size(); i++){
                result += remindConstactsModels.get(i).getUserName();
                if(i < remindConstactsModels.size()-1)
                    result += "、";
            }
            itmeRemindRightText.setText(result);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:                    // 返回
                onBackPressed();
                break;
            case R.id.right_bn:                     // 确定发表
                if(contentEdit.getText().toString().trim().length() == 0&&imagePaths.size()==0){
                    ToasUtil.toast(this, "发表内容不能为空");
                    return;
                }
                if(paramsBean.getType() == 1 || paramsBean.getType() == 2) {
                    loading.show();
                    if (imagePaths.size() > 0) {         // 上传图片
                        paramsBean.setType(2);
                        for(String imgPath : imagePaths){
                            MultipleImageUploader.ImageBean imageBean = new MultipleImageUploader.ImageBean();
                            imageBean.setPath(imgPath);
                            String key = String.format("discover/scoop/pic/%s/%s",
                                    UploadLogic.getTime(),
                                    UploadLogic.getName());
                            imageBean.setKey(key);
                            imageUploader.addImage(imageBean);
                        }
                        imageUploader.uploadImages();
                    } else {
                        paramsBean.setType(1);
                        pushDynamic();                    // 发表动态
                    }
                } else{
                    loading.show();
                    pushDynamic();
                }
                break;
            case R.id.team_circle_link_view:      // 点击链接
                Intent intent = new Intent(this, InfoDataDetailActivity.class);
                intent.putExtra("id", paramsBean.getLinkId());
                intent.putExtra("type", paramsBean.getLinkType());
                intent.putExtra("linkIcon", paramsBean.getLinkIcon());
                intent.putExtra("linkTitle", paramsBean.getLinkTitle());
//                startActivity(intent);
                break;
            case R.id.team_circle_create_level:   // 设置级别
                levelSelectDialog.show();
                break;
            case R.id.team_circle_create_toCompany:     // 发表到
                if(paramsBean.getLevel() == 1) {
                    UserData userData = application.getUserData();
                    companySelectDialog.show(userData == null ? null : userData.getCompanyList());
                    break;
                }
                Intent pushToIntent = new Intent(this, TeamCirclePushToActivity.class);
                pushToIntent.putExtra(COMPANY_ID, companyId);
                pushToIntent.putExtra(COMPANY_NAME, companyName);
                pushToIntent.putExtra(COMPANY_TYPE, companyType);
                pushToIntent.putExtra(TeamCircleSelectVisibilityActivity.VISIBILITY_TYPE, paramsBean.getVisibility());
                pushToIntent.putExtra(TeamCircleSelectVisibilityActivity.TEAM_CIRCLE_CONSTACTS_MODEL_LIST,
                        (Serializable)visibilityConstactsModels);
                pushToIntent.putExtra(TeamCircleSelectVisibilityActivity.TEAM_CIRCLE_COMPANY_MODEL_LIST,
                        (Serializable)visibilityCompanyModels);
                if(paramsBean.getLevel() == 0)
                    pushToIntent.putExtra(TeamCircleSelectVisibilityActivity.HIDE_COMPANY_SELETC, true);
                else
                    pushToIntent.putExtra(TeamCircleSelectVisibilityActivity.HIDE_COMPANY_SELETC, false);
                startActivityForResult(pushToIntent, SET_SENDTO_CODE);
                break;
            case R.id.team_circle_create_visibility:  // 设置谁可见
//                Intent visibilityIntent = new Intent(this, TeamCircleSelectVisibilityActivity.class);
//                visibilityIntent.putExtra(TeamCircleSelectVisibilityActivity.VISIBILITY_TYPE, paramsBean.getVisibility());
//                visibilityIntent.putExtra(TeamCircleSelectVisibilityActivity.TEAM_CIRCLE_CONSTACTS_MODEL_LIST,
//                        (Serializable)visibilityConstactsModels);
//                visibilityIntent.putExtra(TeamCircleSelectVisibilityActivity.TEAM_CIRCLE_COMPANY_MODEL_LIST,
//                        (Serializable)visibilityCompanyModels);
//                if(paramsBean.getLevel() == 0)
//                    visibilityIntent.putExtra(TeamCircleSelectVisibilityActivity.HIDE_COMPANY_SELETC, true);
//                else
//                    visibilityIntent.putExtra(TeamCircleSelectVisibilityActivity.HIDE_COMPANY_SELETC, false);
//                startActivityForResult(visibilityIntent, SET_VISIBILITY_CODE);
                Intent visibilityIntent = new Intent(this, TeamCircleConstactsActivity.class);
                visibilityIntent.putExtra(TeamCircleConstactsActivity.TITLE, "部分可看");
                visibilityIntent.putExtra(TeamCircleConstactsActivity.COMPANY_ID, companyId);
                visibilityIntent.putExtra(TeamCircleConstactsActivity.COMPANY_TYPE, companyType);
                visibilityIntent.putExtra(TeamCircleConstactsActivity.SELECT_CONSTACTS_DATA, selectConstacts.get(companyId));
                startActivityForResult(visibilityIntent, SET_VISIBILITY_CODE);
                break;
            case R.id.team_circle_create_remind:  // 设置 @ 谁
                Intent remindIntent = new Intent(this, TeamCircleConstactsActivity.class);
                remindIntent.putExtra(TeamCircleConstactsActivity.TITLE, "提醒谁看");
                startActivityForResult(remindIntent, SET_REMIND_CODE);
                break;
            case R.id.right_btn:                     // 移除相片对话框
                imagePaths.remove(deletePosition);
                imageAddAdapter.updateAdapter(imagePaths);
                deletePhotoDialog.dismiss();
                break;
            case R.id.team_circle_level_normal:         // 一般级别对话框
                levelSelectDialog.dismiss();
                if(paramsBean.getLevel() == 1)
                    break;
                paramsBean.setLevel(1);
                setLevel(paramsBean.getLevel());
                if(paramsBean.getVisibility() == 1)
                    break;
                companyId = TeamCircleCreateActivity.this.intent.getStringExtra(COMPANY_ID);
                companyType = TeamCircleCreateActivity.this.intent.getIntExtra(COMPANY_TYPE, 1);
                companyName = TeamCircleCreateActivity.this.intent.getStringExtra(COMPANY_NAME);
                paramsBean.setCompanyId(companyId);
                paramsBean.setCompanyType(companyType);
                paramsBean.setVisibility(1);
                itemToCompanyRightText.setText(companyName);
                break;
            case R.id.team_circle_level_important:      // 重要级别对话框
                levelSelectDialog.dismiss();
                paramsBean.setLevel(2);
                setLevel(paramsBean.getLevel());
                break;
            case R.id.team_circle_level_urgent:         // 紧急级别对话框
                levelSelectDialog.dismiss();
                paramsBean.setLevel(3);
                setLevel(paramsBean.getLevel());
                break;
            default:
                return;
        }
    }

    @Override
    public void onBackPressed() {
        if(contentEdit.getText().length() > 0 || imagePaths.size() > 0)
            returnDialog.show();
        else
            finishSelf();
    }

    @Override
    public void itemSelect(int position) {
        if(position == imagePaths.size()){
            ImagePickerIntent intent = new ImagePickerIntent(this);
            // 多选模式
            intent.setSelectModel(SelectModel.MULTI);
            // 是否显示拍照， 默认false
            intent.setShowCarema(true);
            // 最多选择照片数量，默认为9
            intent.setMaxTotal(9);
            // 已选中的照片地址， 用于回显选中状态
            intent.setSelectedPaths((ArrayList<String>)imagePaths);
            // ImageConfig config = new ImageConfig();
            // config.minHeight = 400;
            // config.minWidth = 400;
            // config.mimeType = new String[]{"image/jpeg", "image/png"};   // 图片类型 image/gif ...
            // config.minSize = 1 * 1024 * 1024;                            // 1Mb 图片大小
            // intent.setImageConfig(config);
            startActivityForResult(intent, REQUEST_IMAGE_CODE);
            overridePendingTransition(R.anim.anim_enter, 0);
        } else{
            PhotoPreviewIntent intent = new PhotoPreviewIntent(this);
            intent.setCurrentItem(position);                                // 当前选中照片的下标
            intent.setPhotoPaths((ArrayList<String>)imagePaths);           // 已选中的照片地址
            startActivityForResult(intent, REQUEST_PREVIEW_CODE);
            overridePendingTransition(R.anim.anim_enter, 0);
        }
    }

    /* 发送动态 */
    protected void pushDynamic(){
        paramsBean.setToken(HttpHelper.token);
        paramsBean.setContent(contentEdit.getText().toString());

//        List<TeamCirclePushParams.VisibleBean> visible = new ArrayList<>();
//        if(paramsBean.getVisibility() == 2) {
//            for (TeamCircleConstactsModel visibilityConstactsModel : visibilityConstactsModels) {
//                visible.add(new TeamCirclePushParams.VisibleBean(visibilityConstactsModel.getUserId()));
//            }
//        } else if(paramsBean.getVisibility() == 3){
//            for (SearchResultModel visibilityCompanyModel : visibilityCompanyModels) {
//                visible.add(new TeamCirclePushParams.VisibleBean(visibilityCompanyModel.getId()));
//            }
//        }
//        paramsBean.setVisible(visible);
//
//        List<TeamCirclePushParams.RemindBean> remind = new ArrayList<>();
//        for(TeamCircleConstactsModel remindConstactsModel : remindConstactsModels){
//            remind.add(new TeamCirclePushParams.RemindBean(remindConstactsModel.getUserId()));
//        }
//        paramsBean.setRemind(remind);

        if(paramsBean.getVisibility() == 3
                && visibilityCompanyModels != null && visibilityCompanyModels.size() > 0){  // 发表到其他公司
            paramsBean.setCompanyType(visibilityCompanyModels.get(0).getType());
            paramsBean.setCompanyId(visibilityCompanyModels.get(0).getId());
        } else if(paramsBean.getVisibility() == 1 || paramsBean.getVisibility() == 2){          // 发表到当前公司
            paramsBean.setCompanyType(companyType);
            if(companyType == 2){       // 学校使用classId
                List<OrganizaModel> companyList = application.getUserData().getCompanyList();
                for(OrganizaModel organizaModel : companyList){
                    if(organizaModel.getCompanyId().equals(companyId)){
                        paramsBean.setCompanyId(organizaModel.getClassId());
                        break;
                    }
                }
            } else {
                paramsBean.setCompanyId(companyId);
            }
        }
        if(paramsBean.getLevel() == 1) {                // 不是日常发表
            List<TeamCirclePushParams.VisibleBean> visibleBeen = new ArrayList<>();
            for(TeamCircleConstactsModel remindConstactsModel : remindConstactsModels)
                visibleBeen.add(new TeamCirclePushParams.VisibleBean(remindConstactsModel.getUserId()));
            paramsBean.setVisible(visibleBeen);
            if(paramsBean.getVisible().size() == 0)
                paramsBean.setVisibility(1);            // 全部可见
            else
                paramsBean.setVisibility(2);            // 部分可见
        }

        if(paramsBean.getImgList() != null && paramsBean.getImgList().size() == 0)
            paramsBean.setImgList(null);
        if(paramsBean.getVisible() != null && paramsBean.getVisible().size() == 0)
            paramsBean.setVisible(null);
        if(paramsBean.getRemind() != null && paramsBean.getRemind().size() == 0)
            paramsBean.setRemind(null);

        HttpHelper.getInstance(this).teamCirclePushDynamic(
                JsonUtil.getInstance().entityToJson(paramsBean), PUSH_DYNAMIC_CODE, this);
    }

    private int deletePosition;
    @Override
    public boolean itemLongClick(int position) {
        // 长按提示删除
        if(position < imagePaths.size()){
            deletePosition = position;
            deletePhotoDialog.show();
        }
        return true;
    }

    /* 图片压缩上传回调接口 */
    @Override
    public void imageCompressFail(String key) {
        loading.cancel();
        ToasUtil.toast(this, "抱歉，图片压缩失败，请稍后重试");
    }
    @Override
    public void imageCompressSuccess(String key) {
    }
    @Override
    public void imageCompressComplete() {
    }
    @Override
    public void imageUploadFail(String key) {
        loading.cancel();
        ToasUtil.toast(this, "抱歉，图片上传失败，请稍后重试");
    }
    @Override
    public void imageUploadSuccess(String key, String resultKey) {
    }
    @Override
    public void imageUploadComplete(List<String> resultKeys) {
        List<TeamCirclePushParams.ImgListBean> imgListBeanList = new ArrayList<>();
        for(String img : resultKeys){
            imgListBeanList.add(new TeamCirclePushParams.ImgListBean(img));
        }
        paramsBean.setImgList(imgListBeanList);
        pushDynamic();
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
        } else if(requestType == PUSH_DYNAMIC_CODE) {
            if(paramsBean.getCompanyType() == 2){
                List<OrganizaModel> companyList = application.getUserData().getCompanyList();
                for(OrganizaModel organizaModel : companyList){
                    if(organizaModel.getCompanyType() != 2)
                        continue;
                    if(paramsBean.getCompanyId().equals(organizaModel.getClassId())){
                        paramsBean.setCompanyId(organizaModel.getCompanyId());
                        break;
                    }
                }
            }
            setResult(RESULT_OK, new Intent()
                    .putExtra("companyId", paramsBean.getCompanyId())
                    .putExtra("companyType", paramsBean.getCompanyType()));
            finishSelf();
        }
    }

    public void finishSelf() {
        MyUtil.closeKeyword(this, contentEdit);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 300);
    }
}
